package com.swak.reactivex.transport.quic;

import io.netty.channel.ChannelInitializer;
import io.netty.incubator.codec.quic.QuicStreamChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

/**
 * Quic 协议初始化的设置
 * 
 * @author 618lf
 */
class QuicChannelInitializer extends ChannelInitializer<QuicStreamChannel> {

	@Override
	protected void initChannel(QuicStreamChannel ch) throws Exception {
		new QuicStreamChannelClosedConsumer(ch);
	}

	class QuicStreamChannelClosedConsumer implements GenericFutureListener<Future<Void>> {

		private final QuicStreamChannel channel;

		QuicStreamChannelClosedConsumer(QuicStreamChannel ch) {
			this.channel = ch;
			ch.closeFuture().addListener(this);
		}

		@Override
		public void operationComplete(Future<Void> future) throws Exception {
			if (future.isSuccess()) {
				try {
					channel.parent().close();
				} catch (Exception e) {
				}
			}
		}
	}
}
